package com.hw.springcloud.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hw.springcloud.dao.AccountDao;
import com.hw.springcloud.entity.AccountEntity;
import com.hw.springcloud.service.AccountService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

/**
 * @Classname: AccountServiceImpl
 * @Description: 描述
 * @Date: 2021/12/22
 * @Author: HuWei
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountDao, AccountEntity> implements AccountService {

    @Override
    public Boolean updateAccount(Long userId, BigDecimal used) {
        LambdaQueryWrapper<AccountEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccountEntity::getUserId,userId);
        AccountEntity one = this.getOne(queryWrapper);
        // 计算使用值
        BigDecimal add = one.getUsed().add(used);
        BigDecimal subtract = one.getTotal().subtract(add);
        one.setResidue(subtract);
        one.setUsed(add);
        // 模拟超时异常
        try {
            TimeUnit.SECONDS.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return this.updateById(one);
    }
}
